package com.yaoran.userauth.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yaoran.userauth.dao.OrganizationDao;
import com.yaoran.userauth.entity.Organization;
import com.yaoran.userauth.service.OrganizationService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.ThreadLocalUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrganizationServiceImpl implements OrganizationService {
    private final static Logger logger = LoggerFactory.getLogger(OrganizationServiceImpl.class);
    @Autowired
    private OrganizationDao organizationDao;

    @Override
    public void addOrganization(Organization organization, Date createTime){
        if(createTime != null){
            organization.setCreateTime(createTime);
        }else{
            organization.setCreateTime(new Date());
        }
        organization.setUpdateTime(new Date());
        String name = ThreadLocalUtils.get("operate");
        organization.setCreateBy(name);
        organizationDao.addOrganization(organization);
    }

    @Override
    public PageUtils<Organization> listOrganization(Query query,String keyWord){
        Page<Organization> page = PageHelper.startPage(query.getPage(),query.getLimit());
        List<Organization> list = organizationDao.listOrganization(keyWord);
        int total = (int) page.getTotal();
        return new PageUtils(list,total,query.getLimit(),query.getPage());
    }

    @Override
    public void updateOrganization(Organization organization){
        organization.setUpdateTime(new Date());
        String name = ThreadLocalUtils.get("operate");
        organization.setUpdateBy(name);
        organizationDao.updateOrganization(organization);
    }

    @Override
    public void deleteOrganization(Long id){
        organizationDao.deleteOrganization(id);
    }

    @Override
    public void deleteManyOrganizations(Long[] ids){
        organizationDao.deleteManyOrganizations(ids);
    }

    @Override
    public Organization getOrganization(Long id){
        return organizationDao.getOrganization(id);
    }

    @Override
    public List<Organization> listAllOrganization(){
        return organizationDao.listOrganization();
    }
}
